/**
 * Created with IntelliJ IDEA.
 * Description:寻找峰值
 * User: wangxin
 * Date: 2024-11-01
 * Time: 20:27
 */
public class test1 {
    //方法一
    public int findPeakElement (int[] nums) {
        // write code here
        if(nums.length == 1){
            return 0;
        }
        int i = 0;
        if(nums[i+1] < nums[i]){
            return i;
        }
        i = nums.length -1;
        if( nums[i-1] < nums[i]){
            return i;
        }

        for(i = 1;i < nums.length -1 ;i++){
            if(nums[i-1] < nums[i]&&nums[i+1] < nums[i]){
                return i;
            }
        }
        return -1;
    }
    //方法二
    public int findPeakElement2 (int[] nums) {
        // write code here
        //关键思想：下坡的时候可能找到波峰，但是可能找不到，一直向下走的
        //上坡的时候一定能找到波峰，因为题目给出的是nums[-1] = nums[n] = -∞
        int left = 0;
        int right = nums.length-1;
        while(left<right){
            int mid = left+(right-left)/2;
            //证明右边的路是下坡路，不一定有坡峰
            if(nums[mid]>nums[mid+1]){
                right = mid;
            }
            else{
                //这里是右边的路是上坡路
                left=mid+1;
            }
        }
        return right;
    }
}
